package com.zsck.pos.common;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * activity处理类
 */
public class ActivityManager {

    private List<Activity> activities = new ArrayList<Activity>();

    public static ActivityManager getInstance() {
        return Instance.sin;
    }

    // 在第一次被引用时被加载,// 静态内部类
    static class Instance {
        private static ActivityManager sin = new ActivityManager();

    }

    public void addActivity(Activity activity) {
        if (activity == null) {
            return;
        }
        activities.add(activity);
    }

    public void destoryActivity(Activity activity, boolean needFinish) {
        if (activity == null) {
            return;
        }
        for (int i = 0; i < activities.size(); i++) {
            if (activities.get(i) == activity) {
                if (needFinish) {
                    activities.get(i).finish();
                }
                activities.remove(i);
            }
        }
    }

    public void destoryActivityForName(Activity activity, boolean needFinish) {
        if (activity == null) {
            return;
        }
        for (int i = 0; i < activities.size(); i++) {
            if (activities.get(i).getClass().getSimpleName().equals(activity.getClass().getSimpleName())) {
                if (needFinish) {
                    activities.get(i).finish();
                }
                activities.remove(i);
            }
        }
    }

    public void destoryActivityForName(String activityName, boolean needFinish) {
        if (activityName == null || activityName.equals("")) {
            return;
        }
        for (int i = 0; i < activities.size(); i++) {
            if (activities.get(i).getClass().getSimpleName().equals(activityName)) {
                if (needFinish) {
                    activities.get(i).finish();
                }
                activities.remove(i);
            }
        }
    }

    public boolean hasActivityRunning() {
        if (activities == null || activities.size() == 0) {
            return false;
        }
        return true;
    }

    public void finishAll() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }

    public void finishAllExcept(Class<?>... classes) {
        for (Activity activity : activities) {
            boolean isExcept = false;
            for (Class<?> clazz : classes) {
                if (clazz.getName().equals(activity.getClass().getName())) {
                    isExcept = true;
                    break;
                }
            }
            if (!isExcept) {
                activity.finish();
            }
        }
    }

    public Activity findActivity(Class<?> clazz) {
        if (activities == null)
            return null;
        for (Activity act : activities) {
            if (act.getClass().getName().equals(clazz.getName()))
                return act;
        }
        return null;
    }

    public boolean checkActivity(Class<?> clazz) {
        if (activities == null)
            return false;
        boolean hasOpen = false;
        for (Activity act : activities) {
            if (act.getClass().getSimpleName().equals(clazz.getSimpleName())) {
                hasOpen = true;
            }
        }
        return hasOpen;
    }

    public List<Activity> getActivityList() {
        return activities;
    }


}
